<?php
define( '__ESPACE__', 'admin');
$prefix = '../../';
require_once($prefix.'starter.php');

ob_start();

$PWE_admin = new controler_admin();

if(isset($_GET) && isset($_GET['action']) && $_GET['action']==='delete' && isset($_GET['modele_id']) && $_GET['modele_id']>0){
    $mode = 'delete';
    $user = new modele_user($_GET['modele_id']);
    $user->delete();
}else{
    if(isset($_GET) && isset($_GET['modele_id']) && $_GET['modele_id']>0){
        $mode = 'update';
        $user = new modele_user($_GET['modele_id']);
    }else{
        $mode = 'create';
        $user = new modele_user();
    }
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Ajouter/modifier un utilisateur</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="robots" content="noindex,nofollow">
        <link href='http://fonts.googleapis.com/css?family=Grand+Hotel' rel='stylesheet' type='text/css'>
        <link href="<?php echo $prefix; ?>admin/css/style.css" type="text/css" rel="stylesheet" media="all"/>
        <script src="<?php echo $prefix; ?>js/libs/jquery/jquery.js" type="text/javascript"></script>
        <script src="<?php echo $prefix; ?>js/libs/jquery-validate/jquery.validate.min.js" type="text/javascript"></script>
        <link href="<?php echo $PWE_admin->getUiThemePath(); ?>" type="text/css" rel="stylesheet" media="all"/>
        <script src="<?php echo $prefix; ?>js/libs/jqueryui/jquery-ui.js" type="text/javascript"></script>
        <style>
            label{
                display: inline-block;
                width: 120px;
            }
            div.validatError{
                position: absolute;
                padding: 7px;
                margin: 0 0 0 5px;
                background-color: #FFFFFF;
                border: thin solid #FF0000;
            }
        </style>
<?php if($mode !== 'delete'): ?>
    <?php if(isset($_POST) && isset($_POST['submit'])): ?>
        <?php 
        $datas = $_POST;
        if($_POST['mdp']===''){
            unset($datas['mdp']);
        }
        $user->setDatas($datas);
        if($mode==="update"){
            $user->update();
        }elseif($mode==="create"){
            //$user->debug($user);
            $user->save();
        }
        ?>
        <script type="text/javascript">
            var origine = window.top;    
            $(document).ready(function(){
                origine.closeDialogBox(true);
            });
        </script>
    </head>
    <body>
        <p>Opération terminée</p>
    </body>
    <?php else: ?>
    <script type="text/javascript">
        var origine = window.top;    
        $(document).ready(function(){
            $('.pwe-ui-btn').button();
            // valide form
            var validatorForm = $('#form_user').validate({
                rules:{
                    nom:'required'
                    ,email:{
                        required:true,
                        email:true
                    }
                    <?php if($mode==='create'): ?>
                    ,mdp:'required'            
                    <?php endif; ?>
                }
                ,messages:{
                    email:{
                        required:'Votre email est obligatoire'
                        ,email:'Indiquez une adresse email valide'
                    }
                    ,nom:'Votre nom est obligatoire'
                    <?php if($mode==='create'): ?>
                    ,mdp:'indiquez un mot de passe'            
                    <?php endif; ?>
                }
                ,submitHandler: function(form){
                    form.submit();
                }
                ,invalidHandler: function(){
                    $('div.validatError').remove();
                }
                ,focusInvalid: false
                ,errorElement: "div"
                ,errorClass: "validatError"
                ,errorPlacement: function(error, element) {
                    if($('div#error'+element.attr('name'))){
                        $('div#error'+element.attr('name')).remove();
                    }
                    $('body').append(error);
                    var offset = element.offset();
                    error.attr('id','error'+element.attr('name'));
                    error.css('left', offset.left + element.outerWidth());
                    error.css('top', offset.top);
                    error.click(function(){
                        $(this).fadeOut('fast');
                        $(this).remove();
                    });
                }
            });
            $("input").focus(function() {
                $('div#error' + $(this).attr("name")).remove();
            });
        });
    </script>
    </head>
    <body>
        <form id="form_user" method="post" enctype="multipart/form-data" action="?modele=user&modele_id=<?php echo (isset($_GET['modele_id']))?$_GET['modele_id']:0; ?>">
            <label>Prénom : </label><input id="prenom" type="text" name="prenom" value="<?php echo $user->getPrenom(); ?>" /><br/>
            <label>Nom : </label><input id="nom" type="text" name="nom" value="<?php echo $user->getNom(); ?>" /><br/>
            <label>Email : </label><input id="email" type="text" name="email" value="<?php echo $user->getEmail(); ?>" /><br/>
            <label>Mot de passe : </label><input id="mdp" type="password" name="mdp" value="" /><br/>
            <label>Adresse 1 : </label><input id="adresse1" type="text" name="adresse1" value="<?php echo ($user->getAdresse()!==null)?$user->getAdresse()->getAdresse1():''; ?>" /><br/>
            <label>Adresse 2 : </label><input id="adresse2" type="text" name="adresse2" value="<?php echo ($user->getAdresse()!==null)?$user->getAdresse()->getAdresse2():''; ?>" /><br/>
            <label>Code postal : </label><input id="cp" type="text" name="cp" value="<?php echo ($user->getAdresse()!==null)?$user->getAdresse()->getCp():''; ?>"/><br/>
            <label>Ville : </label><input id="ville" type="text" name="ville" value="<?php echo ($user->getAdresse()!==null)?$user->getAdresse()->getVille():''; ?>" /><br/>
            <label>Accès : </label><select id="level" name="user_level_id">
                <?php foreach(modele_user::getListLevel() as $level): ?>
                <?php if(modele_user::getConnectedAdmin()->getLevel()<=$level['level']): ?>
                <option value="<?php echo $level['id']; ?>" <?php echo ($user->getLevelId()===$level['id'])?'selected="selected"':'' ?>><?php echo $level['nom']; ?></option>
                <?php endif; ?>
                <?php endforeach; ?>
            </select><br/>
            <div align="right"><input class="pwe-ui-btn" type="submit" name="submit" value="valider" /></div>
        </form>
    </body>
    <?php endif; ?>
</html>
<?php else: ?>
    <script type="text/javascript">
        var origine = window.top;    
        $(document).ready(function(){
            origine.closeDialogBox(true);
        });
    </script>
    </head>
    <body>
        suppression terminée
    </body>
</html>
<?php endif; ?>
<?php ob_end_flush(); ?>